//
//  MainTabBar.swift
//  新浪微博
//
//  Created by macbook on 16/3/6.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    
    
    override init(frame: CGRect) {
        
        super.init(frame: CGRectZero)
        addSubview(plusBtn)
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        //注销改行代码 就会支持从xib/sb中加载数据
       // fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    

    override func layoutSubviews() {
        super.layoutSubviews()
        //计算frame
        let w = UIScreen.mainScreen().bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //第一一个递增索引
        var index: CGFloat = 0
        for subview in subviews{
        if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
            //修改subView的frame
            subview.frame = CGRectOffset(rect, index * w, 0)
            if index == 1 {
                //多加一次
                index++
              
               }
       
            index++
            }
        
        }
        addSubview(plusBtn)
        plusBtn.frame = CGRectOffset(rect, 2 * w, 0)

    }
    
    //MARK 懒加载子视图
    lazy var plusBtn : UIButton = {
        
    //UIButton()就是自定义样式
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
   // btn.sizeToFit()
    
    return btn
    
    }()
    
    
    
    
    
    
    
}
